package com.baozun.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baozun.mapper.TPhotoMapper;
import com.baozun.model.TPhoto;
import com.baozun.model.TPhotoExample;
import com.baozun.service.PhotoService;
import com.baozun.util.PageData;

@Service("photoService")
@Transactional
public class PhotoServiceImpl implements PhotoService {
    
    @Autowired
    private TPhotoMapper photoMapper;
    
    @Override
    public void add(TPhoto photo) {
        if(StringUtils.isEmpty(photo.getStPhotoId())){
            photo.setStPhotoId(UUID.randomUUID().toString());
        }
        photo.setDtCreateTime(new Date());
        photoMapper.insert(photo);
    }
    
    @Override
    public TPhoto get(String stPhotoId) {
        return photoMapper.selectByPrimaryKey(stPhotoId);
    }
    
    @Override
    public void delete(String stPhotoId) {
        photoMapper.deleteByPrimaryKey(stPhotoId);
    }

    @Override
    public void update(TPhoto bean) {
       bean.setDtUpdateTime(new Date());
       photoMapper.updateByPrimaryKeySelective(bean);
    }

    @Override
    public PageData<TPhoto> getPageListByExample(TPhotoExample record, int pageSize, int currentPage) {
        record.setSize(pageSize);
        record.setStart(pageSize * (currentPage - 1));
        PageData<TPhoto> infoList = new PageData<TPhoto>();
        List<TPhoto> appendixList = photoMapper.selectByExample(record);
        int count = photoMapper.countByExample(record);
        infoList.addAll(appendixList);
        infoList.setPage(currentPage);
        infoList.setPages((count + pageSize - 1) / pageSize);
        infoList.setPageSize(pageSize);
        infoList.setTotalRecords(count);
        return infoList;
    }

    @Override
    public List<TPhoto> getList(TPhotoExample record) {
        return photoMapper.selectByExample(record);
    }
    
}
